package com.dxf.hchat.controller;

import com.dxf.hchat.pojo.TbUser;
import com.dxf.hchat.pojo.vo.Result;
import com.dxf.hchat.pojo.vo.User;
import com.dxf.hchat.service.UserService;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpRequest;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

@Controller
@RequestMapping("/user")
@ResponseBody
public class UserController {

    @Autowired
    private UserService userService;

    @RequestMapping("findAll")
    public List<TbUser> findAll() {
        List<TbUser> all = userService.findAll();

        return all;
    }


    @RequestMapping("/login")
    public Result login(@RequestBody TbUser tbUser) {


        try {
            if (tbUser.getUsername() != null && tbUser.getPassword() != null) {
                User user = userService.login(tbUser.getUsername(), tbUser.getPassword());
                if (user != null) {
                    return new Result(true, "登录成功", user);
                }

            }

            return new Result(false, "登录失败");
        } catch (Exception e) {
            return new Result(false, "登录错误");
        }

    }

    @RequestMapping("/register")
    public Result register(@RequestBody TbUser tbUser) {

        if (tbUser.getClientId() == null || tbUser.getUsername() == null || tbUser.getPassword() == null || tbUser == null) {
            return new Result(false, "注册失败");
        }

        return userService.register(tbUser);

    }


    @RequestMapping("/upload")
    public Result upload(MultipartFile file, String userid) {

        User user = userService.upload(file, userid);
        System.out.println(user);
        try {
            if (user != null) {
                return new Result(true, "上传成功", user);
            } else {
                return new Result(false, "上传失败");
            }
        } catch (Exception e) {
            return new Result(false, "上传失败");
        }


    }

    @RequestMapping("/updateNickname")
    public Result updateNickname(@RequestBody TbUser tbUser) {

        User user = userService.updateNickname(tbUser.getId(), tbUser.getNickname());
        System.out.println(user);
        try {
            if (user != null) {
                return new Result(true, "修改昵称成功", user);
            } else {
                return new Result(false, "修改昵称失败");
            }
        } catch (Exception e) {
            return new Result(false, "修改昵称失败");
        }

    }

    @RequestMapping("/findById")
    public User findById(String userid) {

        try {
            User user = userService.findById(userid);
            if (user != null) {
                return user;
            } else {
                return null;
            }
        } catch (Exception e) {
            return null;
        }
    }

    @RequestMapping("/findByUsername")
    public Result findByUsername(String userid, String friendUsername) {

        try {
            User user = userService.findByUsername(userid, friendUsername);
            if (user != null) {
                return new Result(true, "查询user成功", user);
            } else {
                return new Result(false, "找不到user");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return new Result(false, "出现错误");
        }

    }
}
